package com.yanqu.road.dao.impl.crosschannel;

import com.alibaba.fastjson.JSON;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDao;
import com.yanqu.road.entity.player.UserBaseInfo;
import com.yanqu.road.entity.rank.xrank.XSeasonUserRank;

import java.math.BigInteger;
import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class XSeasonUserRankDaoImpl extends TempDao {

    private String dbName;
    private String tableName;

    public XSeasonUserRankDaoImpl(String dbName, String tableName) {
        this.dbName = dbName;
        this.tableName = tableName;
    }

    public List<XSeasonUserRank> getXSeasonUserRankList(int systemId, int seasonId, int type, String rangeKey) {

        String getXSeasonUserRankSql = "select * from `" + tableName + "` where `systemId`=? and `seasonId`=? and `conditionType`=? and `rangeKey`=?";

        List<XSeasonUserRank> list = new ArrayList<>();

        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(dbName);
            call = getDb().getConnection().prepareCall(getXSeasonUserRankSql);
            int i = 1;
            call.setInt(i++, systemId);
            call.setInt(i++, seasonId);
            call.setInt(i++, type);
            call.setString(i++, rangeKey);
            rs = call.executeQuery();
            while (rs.next()) {
                list.add(parse(rs));
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return list;
    }

    public boolean replaceXSeasonUserRank(List<XSeasonUserRank> rankList, Set<Integer> dbSet) {
        String replaceSql = "replace into `" + tableName + "`(systemId,seasonId,conditionType,rangeKey,rank,channelId,serverId,userId,baseInfo,param,value,lastUpdateTime) values (?,?,?,?,?,?,?,?,?,?,?,?)";
        try {
            getDb().setConnName(dbName);
            List<Map<Integer, DbParameter>> mapList = new ArrayList<>();
            int rank = 0;
            for (XSeasonUserRank xUserRank : rankList) {
                rank++;
                if (!dbSet.contains(rank)) {
                    continue;
                }
                int i = 1;
                Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
                para.put(i++, new DbParameter(Types.INTEGER, xUserRank.getSystemId()));
                para.put(i++, new DbParameter(Types.INTEGER, xUserRank.getSeasonId()));
                para.put(i++, new DbParameter(Types.INTEGER, xUserRank.getConditionType()));
                para.put(i++, new DbParameter(Types.VARCHAR, xUserRank.getRangeKey()));
                para.put(i++, new DbParameter(Types.INTEGER, rank));
                para.put(i++, new DbParameter(Types.INTEGER, xUserRank.getChannelId()));
                para.put(i++, new DbParameter(Types.BIGINT, xUserRank.getServerId()));
                para.put(i++, new DbParameter(Types.BIGINT, xUserRank.getUserId()));
                para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(xUserRank.getBaseInfo())));
                para.put(i++, new DbParameter(Types.VARCHAR, xUserRank.getParam()));
                para.put(i++, new DbParameter(Types.VARCHAR, xUserRank.getValue().toString()));
                para.put(i++, new DbParameter(Types.BIGINT, xUserRank.getLastUpdateTime()));
                mapList.add(para);
            }
            if (getDb().executeBatchQuery(replaceSql, mapList) > -1) {
                return true;
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        }
        return false;
    }

    protected XSeasonUserRank parse(ResultSet rs) throws SQLException {
        XSeasonUserRank xUserRank = new XSeasonUserRank();
        xUserRank.setSystemId(rs.getInt("systemId"));
        xUserRank.setSeasonId(rs.getInt("seasonId"));
        xUserRank.setConditionType(rs.getInt("conditionType"));
        xUserRank.setRangeKey(rs.getString("rangeKey"));
        xUserRank.setChannelId(rs.getInt("channelId"));
        xUserRank.setServerId(rs.getLong("serverId"));
        xUserRank.setUserId(rs.getLong("userId"));
        xUserRank.setBaseInfo(JSON.parseObject(rs.getString("baseInfo"), UserBaseInfo.class));
        xUserRank.setParam(rs.getString("param"));
        xUserRank.setValue(new BigInteger(rs.getString("value")));
        xUserRank.setLastUpdateTime(rs.getLong("lastUpdateTime"));
        return xUserRank;
    }

}
